package com.rockcent.third.controller;

import com.rockcent.third.Routes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

/**
 * Created by oyhk on 16/7/11.
 */
@Controller
public class MainController {

    private final Logger log = LoggerFactory.getLogger(MainController.class);

    @RequestMapping(value = Routes.MAIN_PREFIX, method = RequestMethod.GET)
    public String index(HttpServletRequest request) {
        if(this.isMobile(request)){
            log.info("======================>isMobile : true");
            return "main/mobile/index";
        }
        return "main/index";
    }

    @RequestMapping(value = Routes.MAIN_ABOUT, method = RequestMethod.GET)
    public String about(HttpServletRequest request) {
        if(this.isMobile(request)){
            log.info("======================>isMobile : true");
            return "main/mobile/about";
        }
        return "main/about";
    }

    @RequestMapping(value = Routes.MAIN_SERVICE, method = RequestMethod.GET)
    public String service(HttpServletRequest request) {
        if(this.isMobile(request)){
            log.info("======================>isMobile : true");
            return "main/mobile/service";
        }
        return "main/service";
    }

    @RequestMapping(value = Routes.MAIN_SOLUTION_FINANCE, method = RequestMethod.GET)
    public String solution_finance(HttpServletRequest request) {
        if(this.isMobile(request)){
            log.info("======================>isMobile : true");
            return "main/mobile/solution_finance";
        }
        return "main/solution_finance";
    }

    @RequestMapping(value = Routes.MAIN_SOLUTION_MEDIA, method = RequestMethod.GET)
    public String solution_media(HttpServletRequest request) {
        if(this.isMobile(request)){
            log.info("======================>isMobile : true");
            return "main/mobile/solution_media";
        }
        return "main/solution_media";
    }

    @RequestMapping(value = Routes.MAIN_SOLUTION_COMMERCE, method = RequestMethod.GET)
    public String solution_commerce(HttpServletRequest request) {
        if(this.isMobile(request)){
            log.info("======================>isMobile : true");
            return "main/mobile/solution_commerce";
        }
        return "main/solution_commerce";
    }

    @RequestMapping(value = Routes.MAIN_SOLUTION_TRADE, method = RequestMethod.GET)
    public String solution_trade(HttpServletRequest request) {

        if(this.isMobile(request)){
            log.info("======================>isMobile : true");
            return "main/mobile/solution_trade";
        }
        return "main/solution_trade";
    }

    @RequestMapping(value = Routes.MAIN_PROTOCOL, method = RequestMethod.GET)
    public String protocol() {
        return "main/protocol";
    }

    @RequestMapping(value = Routes.MAIN_PRODUCTS, method = RequestMethod.GET)
    public String products(HttpServletRequest request) {
        if(this.isMobile(request)){
            log.info("======================>isMobile : true");
            return "main/mobile/products";
        }
        return "main/products";
    }

    @RequestMapping(value = Routes.MAIN_SOLUTION, method = RequestMethod.GET)
    public String solution(HttpServletRequest request) {
        if(this.isMobile(request)){
            log.info("======================>isMobile : true");
            return "main/mobile/solution";
        }
        return "main/solution";
    }


    /**
     * 是否为手机
     * @param request
     * @return
     */
    public boolean isMobile(HttpServletRequest request) {
        String userAgent = request.getHeader("user-agent");
        log.info("======================>userAgent : {}",userAgent);
        List<String> stringList = Arrays.asList("Android", "iPhone", "iPod", "iPad", "Windows Phone", "MQQBrowser");
        for (int i = 0; i < stringList.size(); i++) {
            String o = stringList.get(i);
            if (userAgent.indexOf(o) > 0) {
                return true;
            }

        }
        return false;
    }
}